function throttle(fn, delay) {
    let valid = true
    return function (e) {
        if (!valid) {
            //休息时间 暂不接客
            return false
        }
        // 工作时间，执行函数并且在间隔期内把状态位设为无效
        valid = false
        setTimeout(() => {
            fn(e)
            valid = true;
        }, delay)
    }
}

function menu() {
    let m = document.querySelector('.menu')
    let nav = document.querySelector('header .nav')
    let f = true
    m.addEventListener('click', function () {
        if (f) {
            nav.style.display = 'block'
            f = false
        } else {
            nav.style.display = 'none'
            f = true
        }
    })
    document.addEventListener("scroll", throttle(function () {
        if (document.documentElement.clientWidth < 420) {
            nav.style.display = 'none'
            f = true
        }
    }, 200))
    window.addEventListener("resize", throttle(function (e) {
        if (document.documentElement.clientWidth > 420) {
            nav.style.display = 'block'
            f = false
        }

    }, 200))
}

(function () {
    let navTop = document.querySelector('.nav-top')
    let cdTop = document.querySelector('#cd-top')
    let searchBtn = document.querySelector('.search>a')
    let searchBox = document.querySelector('.search-box')
    let quit = document.querySelector('.search-box .quit')

    setInterval(function () {
        if (document.documentElement.scrollTop > 0) {
            navTop.classList.add("show")
            cdTop.className = "down"
        } else {
            navTop.classList.remove("show")
            cdTop.className = ""
        }
    }, 200)


    cdTop.addEventListener('click', function () {
        let t = setInterval(function () {
            if (document.documentElement.scrollTop <= 0) {
                clearInterval(t)
            }
            document.documentElement.scrollTop -= 100
        }, 20)
    })

    searchBtn.addEventListener('click', function () {
        searchBox.classList.toggle('block')
    })
    quit.addEventListener('click', function () {
        searchBox.classList.toggle('block')
    })

    menu()
})()